﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.Admin.UserManage.Controls
{
	public partial class AddUser : BaseUserControl
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
				this.DataBind();
		}


		/// <summary>
		/// 属性 Password
		/// </summary>
		public string Password
		{
			get
			{
				return Convert.ToString(CtrlPassword.Text);
			}
			set
			{
				CtrlPassword.Text = value.ToString();
			}
		}

		/// <summary>
		/// 属性 Email
		/// </summary>
		public string Email
		{
			get
			{
				if (String.IsNullOrEmpty(CtrlEmail.Text))
					return null;

				return Convert.ToString(CtrlEmail.Text);
			}
			set
			{
				if (value == null)
					CtrlEmail.Text = "";
				else
					CtrlEmail.Text = value.ToString();
			}
		}

		/// <summary>
		/// 属性 PasswordQuestion
		/// </summary>
		public string PasswordQuestion
		{
			get
			{
				if (String.IsNullOrEmpty(CtrlPasswordQuestion.Text))
					return null;

				return Convert.ToString(CtrlPasswordQuestion.Text);
			}
			set
			{
				if (value == null)
					CtrlPasswordQuestion.Text = "";
				else
					CtrlPasswordQuestion.Text = value.ToString();
			}
		}

		/// <summary>
		/// 属性 PasswordAnswer
		/// </summary>
		public string PasswordAnswer
		{
			get
			{
				if (String.IsNullOrEmpty(CtrlPasswordAnswer.Text))
					return null;

				return Convert.ToString(CtrlPasswordAnswer.Text);
			}
			set
			{
				if (value == null)
					CtrlPasswordAnswer.Text = "";
				else
					CtrlPasswordAnswer.Text = value.ToString();
			}
		}

		/// <summary>
		/// 属性 UserName
		/// </summary>
		public string UserName
		{
			get
			{
				return Convert.ToString(CtrlUserName.Text);
			}
			set
			{
				CtrlUserName.Text = value.ToString();
			}
		}


		/// <summary>
		/// 控件的 UserInfo 实例。
		/// </summary>
		public User Info
		{
			get
			{
				User user = new User();

				user.Password = Password;
				user.Email = Email;
				user.PasswordQuestion = PasswordQuestion;
				user.PasswordAnswer = PasswordAnswer;
				user.UserName = UserName;

				return user;
			}
			set
			{
				if (value == null) return;

				Password = value.Password;
				Email = value.Email;
				PasswordQuestion = value.PasswordQuestion;
				PasswordAnswer = value.PasswordAnswer;
				UserName = value.UserName;
			}
		}

		protected void CVUserName_ServerValidate(object source, ServerValidateEventArgs args)
		{
			if (Users.CheckUserExists(UserName))
			{
				args.IsValid = false;
			}
			else
			{
				args.IsValid = true;
			}
		}
	}
}